package at.socialconference.web.client.ui;

import java.util.Date;
import java.util.List;

import at.socialconference.inter.connection.protocol.SocketProtocol;
import at.socialconference.inter.model.Place;
import at.socialconference.inter.model.Talk;
import at.socialconference.web.client.SCwebService;
import at.socialconference.web.client.util.AsynCallbackInsertObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DatePicker;

public class AddTalkPopup extends PopupPanel implements ClickHandler {
	private VerticalPanel content;
	private FlexTable flexTable;
	
	
	private TextBox txtName = new TextBox();
	
	private DatePicker date = new DatePicker();
	private TextBox dateFromH = new TextBox();
	private TextBox dateFromM = new TextBox();
	private TextBox dateToH = new TextBox();
	private TextBox dateToM = new TextBox();
	
	private TextBox txtDescription = new TextBox();
	
	private ListBoxPlaces lbPlaces = new ListBoxPlaces();
	private AdminHome adminHome;
	
	private Button btnSubmit = new Button("Add");
	private Button btnCancel = new Button("Cancel");
	
	//TODO add speakers
	
	private class AsyncCallbackInsertTalk extends AsynCallbackInsertObject {
		@Override
		public void onSuccess(Integer result) {
			super.onSuccess(result);
			if (result==SocketProtocol.OK) {
				AddTalkPopup.this.adminHome.updateTalks();
			}
		}
	}
	
	private class AsyncCallbackGetPlaces implements AsyncCallback<List<Place>> {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Could not reach server");
		}
		@Override
		public void onSuccess(List<Place> result) {
			if (result!=null && result.size()>0) {
				for (Place p : result) {
					lbPlaces.addPlace(p);
				}
			} else {
				Window.alert("Could not fetch places from DB");
				AddTalkPopup.this.hide();
			}
		}
	}
	
	public AddTalkPopup(AdminHome adminHome) {
		this.adminHome = adminHome;
		//fetch places from DB
		SCwebService.Util.getInstance().getPlaces(new AsyncCallbackGetPlaces());
		
		date.setValue(new Date());
		
		content = new VerticalPanel();
		add(content);
		
		flexTable = new FlexTable();
		content.add(flexTable);
		
		int i=-1;
		
		flexTable.setWidget(++i, 0, new Label("Name:"));
		flexTable.setWidget(i, 1, txtName);
		
		flexTable.setWidget(++i, 0, new Label("Date"));
		flexTable.setWidget(i, 1, date);
		
		HorizontalPanel hpFrom = new HorizontalPanel();
		hpFrom.add(dateFromH);
		hpFrom.add(new Label(":"));
		hpFrom.add(dateFromM);
		flexTable.setWidget(++i, 0, new Label("From:"));
		flexTable.setWidget(i, 1, hpFrom);
		
		HorizontalPanel hpTo = new HorizontalPanel();
		hpTo.add(dateToH);
		hpTo.add(new Label(":"));
		hpTo.add(dateToM);
		flexTable.setWidget(++i, 0, new Label("To:"));
		flexTable.setWidget(i, 1, hpTo);
		
		flexTable.setWidget(++i, 0, new Label("Description:"));
		flexTable.setWidget(i, 1, txtDescription);
		
		flexTable.setWidget(++i, 0, new Label("Places:"));
		flexTable.setWidget(i, 1, lbPlaces);
		
		
		HorizontalPanel submitPanel = new HorizontalPanel();
		submitPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		submitPanel.add(btnSubmit);
		
		HorizontalPanel cancelPanel = new HorizontalPanel();
		submitPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		submitPanel.add(btnCancel);
		
		flexTable.setWidget(++i, 0, submitPanel);
		flexTable.setWidget(i, 1, cancelPanel);
		
		btnSubmit.addClickHandler(this);
		btnCancel.addClickHandler(this);
		
		this.setGlassEnabled(true);
		this.center();
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource().equals(btnSubmit)) {
			Talk talk = new Talk();
			try {
				Date timeFrom = DateTimeFormat.getFormat("HH:mm").parse(dateFromH.getText()+":"+dateFromM.getText());
				Date timeTo = DateTimeFormat.getFormat("HH:mm").parse(dateToH.getText()+":"+dateToM.getText());

				Place place = lbPlaces.getSelectedPlace();
				
				talk.setTitle(txtName.getText());
				talk.setDescription(txtDescription.getText());
				
				talk.setDate(date.getValue());
				talk.setFrom(timeFrom);
				talk.setTo(timeTo);
				talk.setPlace(place);
				SCwebService.Util.getInstance().insertObject(talk, new AsyncCallbackInsertTalk());
				this.hide();
			}catch (Exception e) {
				Window.alert("Error");
				Window.alert(e.getLocalizedMessage());
			}
		}
		if (event.getSource().equals(btnCancel)) {
			this.hide();
		}
		
	}
}
